package com.xpc.rpc.loadbalancer.random;

import com.xpc.rpc.annotation.SPIClass;
import com.xpc.rpc.loadbalancer.api.ServiceLoadBalancerApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Random;

@SPIClass
public class RamdomLoadbalancer<T> implements ServiceLoadBalancerApi<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(RamdomLoadbalancer.class);

    @Override
    public T select(List<T> services) {
        LOGGER.info("这是随机负载均衡策略..........................");
        if(services == null || services.isEmpty())  {
            return null;
        }
        Random random = new Random();
        int index = random.nextInt(services.size());
        return services.get(index);
    }
}
